Ext.define('SuggestBoxView', {
	extend : 'Ext.panel.Panel',
	id : 'SuggestBoxView',
	title : this.title ? this.title : '意见箱管理',
	iconCls : 'menu-suggestbox',
	layout : 'border',

	initComponent : function() {
		this.searchPanel = new KE.SearchPanel({
					region : 'north',
					items : [{
								text : '标题'
							}, {
								name : 'Q_subject_S_LK',
								xtype : 'textfield',
								width : 150
							}, {
								text : '日期'
							}, {
								name : 'Q_createtime_D_GE',
								xtype : 'datefield',
								format : 'Y-m-d',
								width : 100
							}, {
								text : '至'
							}, {
								name : 'Q_createtime_D_LE',
								xtype : 'datefield',
								format : 'Y-m-d',
								width : 100
							}, {
								text : '发送人'
							}, {
								name : 'Q_senderFullname_S_LK',
								xtype : 'textfield',
								width : 100
							}, {
								text : '是否公开'
							}, {
								hiddenName : 'Q_isOpen_SN_EQ',
								xtype : 'combo',
								editable : false,
								width : 80,
								mode : 'local',
								triggerAction : 'all',
								store : [['', '全部'], ['0', '公开'], ['1', '未公开']]
							}, {
								xtype : 'button',
								text : '查询',
								iconCls : 'search',
								handler : this.search
							}, {
								xtype : 'button',
								text : '清空',
								scope : this,
								iconCls : 'reset',
								handler : this.reset
							}, {
								name : 'Q_senderId_SN_EQ',
								id : 'SuggestBoxViewSearchSenderId',
								xtype : 'hidden'
							}]
				});
		this.topbar = new Ext.Toolbar({
					items : [{
								iconCls : 'btn-add',
								text : '添加',
								xtype : 'button',
								handler : this.createRecord
							}]
				});
		if (!this.isOutSide) {
			this.topbar.add({
						iconCls : 'btn-del',
						text : '删除',
						xtype : 'button',
						handler : this.delRecords,
						scope : this
					});
		}
		this.gridPanel = new KE.GridPanel({
			region : 'center',
			tbar : this.topbar,
			id : 'SuggestBoxGrid',
			url : __ctxPath + "/info/listSuggestBox.do",
			baseParams : {
				'Q_senderId_L_EQ' : this.userId
			},
			sorters : [{
						property : 'boxId',
						direction : 'DESC'
					}],
			fields : [{
						name : 'boxId',
						type : 'int'
					}, 'subject', 'content', 'createtime', 'recUid',
					'recFullname', 'senderId', 'senderFullname', 'senderIp',
					'phone', 'email', 'isOpen', 'replyContent', 'replyTime',
					'replyId', 'replyFullname', 'status', 'queryPwd'],
			columns : [{
						header : 'boxId',
						dataIndex : 'boxId',
						hidden : true
					}, {
						header : '是否公开',
						width : 80,
						dataIndex : 'isOpen',
						renderer : function(value) {
							if (value == 0) {
								return '<font color="green">公开</font>';
							} else {
								return '<font color="red">未公开</font>';
							}
						}
					}, {
						header : '状态',
						width : 80,
						dataIndex : 'status',
						renderer : function(value) {
							if (value == 0) {
								return '<font color="blue">草稿</font>';
							} else if (value == 1) {
								return '<font color="red">提交</font>';
							} else {
								return '<font color="green">已回复</font>';
							}
						}
					}, {
						header : '查询密码',
						wdith : 80,
						dataIndex : 'queryPwd',
						renderer : function(value, metadata, record) {
							var isOpen = record.data.isOpen;
							if (value != null && isOpen == 1) {
								return '<font color="red">需要密码</font>';
							} else {
								return '<font color="green">无密码</font>';
							}
						}
					}, {
						header : '意见标题',
						width : 300,
						dataIndex : 'subject'
					}, {
						header : '创建日期',
						dataIndex : 'createtime',
						renderer : function(value) {
							return value.substring(0, 10);
						}
					}, {
						header : '发送人',
						dataIndex : 'senderFullname',
						renderer : function(value) {
							if (value != null && value != '') {
								return value;
							} else {
								return '匿名';
							}
						}
					}, {
						header : '发送人IP',
						dataIndex : 'senderIp'
					}, {
						header : '管理',
						width : 200,
						scope: this,
						renderer : function(value, metadata, record) {
							var status = record.data.status;
							var isOpen = record.data.isOpen;
							var queryPwd = record.data.queryPwd;
							var needPwd = false;
							if (isOpen == 1 && queryPwd != null && queryPwd != '') {
								needPwd = true;
							}
							var editId = record.data.boxId;
							var str = '';
							if (!this.isOutSide) {
								str = '<button title="删除" value=" " class="btn-del" onclick="SuggestBoxView.delByIds('
										+ editId + ')">&nbsp;</button>';
								if (status == 0) {
									str += '&nbsp;&nbsp;&nbsp;<button title="编辑" value=" " class="btn-edit" onclick="SuggestBoxView.editRecord('
											+ editId + ')">&nbsp;</button>';
								} else if (status == 1 && this.userId == null) {
									str += '&nbsp;&nbsp;&nbsp;<button title="回复" value=" " class="btn-suggest-reply" onclick="SuggestBoxView.reply('
											+ editId + ')">&nbsp;</button>';
								}
							}
							str += '&nbsp;&nbsp;&nbsp;<button title="浏览" value=" " class="btn-suggest-scan" onclick="SuggestBoxView.scan('
									+ editId + ',' + needPwd + ')">&nbsp;</button>';

							return str;
						}
					}]
		});

		this.items = [this.searchPanel, this.gridPanel];
		this.callParent();
		this.gridPanel.addListener('rowdblclick', this.rowdblclick);
	},

	// 重置查询表单
	reset : function() {
		this.searchPanel.getForm().reset();
		// Ext.each(this.searchPanel.query('combo'), function(c) {
		// c.setValue('');
		// });
	},
	// 按条件搜索
	search : function() {
		if (self.searchPanel.getForm().isValid()) {// 如果合法
			$search({
						searchPanel : this.searchPanel,
						gridPanel : this.gridPanel
					});
		}
	},
	rowdblclick : function(grid, rec) {
		var status = rec.data.status;
		var isOpen = rec.data.isOpen;
		var queryPwd = rec.data.queryPwd;
		var needPwd = false;
		if (isOpen == 1 && queryPwd != null && queryPwd != '') {
			needPwd = true;
		}
		if (status == 0) {
			new SuggestBoxForm({
						boxId : rec.data.boxId
					}).show();
		} else {
			SuggestBoxView.scan(rec.data.boxId, needPwd);
		}
	},
	// 添加记录
	createRecord : function() {
		new SuggestBoxForm().show();
	},
	// 删除多条记录
	delRecords : function() {
		var gridPanel = Ext.getCmp('SuggestBoxGrid');
		var selectRecords = gridPanel.getSelectionModel().getSelection();
		if (selectRecords.length == 0) {
			Ext.ux.Toast.msg("信息", "请选择要删除的记录！");
			return;
		}
		var ids = Array();
		for (var i = 0; i < selectRecords.length; i++) {
			ids.push(selectRecords[i].data.boxId);
		}
		SuggestBoxView.delByIds(ids);
	}
});

SuggestBoxView.delByIds = function(ids) {
	Ext.Msg.confirm('信息确认', '您确认要删除所选记录吗？', function(btn) {
				if (btn == 'yes') {
					Ext.Ajax.request({
								url : __ctxPath + '/info/multiDelSuggestBox.do',
								params : {
									ids : ids
								},
								method : 'POST',
								success : function(response, options) {
									Ext.ux.Toast.msg('操作信息', '成功删除该[SuggestBox]！');
									Ext.getCmp('SuggestBoxGrid').getStore().reload();
								},
								failure : function(response, options) {
									Ext.ux.Toast.msg('操作信息', '操作出错，请联系管理员！');
								}
							});
				}
			});// end of comfirm
};
/**
 * 编辑
 * 
 * @param {}
 *            id
 */
SuggestBoxView.editRecord = function(id) {
	new SuggestBoxForm({
				boxId : id
			}).show();
};
/**
 * 回复
 */
SuggestBoxView.reply = function(id) {
	new SuggestBoxReplyForm({
				boxId : id
			}).show();
};
/**
 * 浏览
 */
SuggestBoxView.scan = function(id, needPwd) {
	if (needPwd) {
		var form = new Ext.FormPanel({
					layout : 'form',
					bodyStyle : 'padding:10px',
					border : false,
					url : __ctxPath + '/info/matchSuggestBox.do',
					defaults : {
						anchor : '98%,98%'
					},
					items : [{
								fieldLabel : '访问密码',
								name : 'suggestBox.queryPwd',
								xtype : 'textfield',
								inputType : 'password'
							}, {
								name : 'suggestBox.boxId',
								value : id,
								xtype : 'hidden'
							}]
				})
		var win = new Ext.Window({
					layout : 'fit',
					iconCls : 'btn-add',
					items : form,
					modal : true,
					minHeight : 149,
					minWidth : 499,
					height : 150,
					width : 500,
					maximizable : true,
					title : '访问密码',
					buttonAlign : 'center',
					buttons : [{
						text : '确定',
						iconCls : 'btn-save',
						handler : function() {
							if (form.getForm().isValid()) {
								form.getForm().submit({
											method : 'POST',
											waitMsg : '正在提交数据...',
											success : function(fp, action) {
												win.close();
												new SuggestBoxDisplay({boxId : id}).show();
											},
											failure : function() {
												Ext.ux.Toast.msg('操作信息',
														'访问密码不正确！');
											}
										})
							}
						}
					}, {
						text : '取消',
						iconCls : 'btn-cancel',
						handler : function() {
							win.close();
						}
					}]
				});
		win.show();
	} else {
		new SuggestBoxDisplay({
					boxId : id
				}).show();
	}

};